using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace NetQueryLib
{
	internal static class DebuggingExtensions
	{
		public static string GetPath(this XElement element)
		{
			var nodePath = new StringBuilder();

			var parents = element.Ancestors().Reverse();

			foreach (var parent in parents)
				nodePath.Append(parent.Name.LocalName + " > ");

			nodePath.Append(element.Name.LocalName + GetAttributes(element));

			return nodePath.ToString();
		}

		public static string GetAttributes(this XElement element)
		{
			string[] attributes = element.Attributes().Select(attr => String.Format("{0}=\"{1}\"", attr.Name, attr.Value)).ToArray();

			if (attributes.Length == 0)
				return String.Empty;

			return "[" + String.Join(",", attributes) + "]";
		}
	}
}